import datetime

from mlwrite import model, db


class TContextualLibrary(model.BaseModel):
    __bind_key__ = 'writemall'
    __tablename__ = 't_contextual_library'
    id = db.Column(db.Integer, primary_key=True, index=True)
    parent_id = db.Column(db.Integer, nullable=False)
    group_id = db.Column(db.Integer, nullable=False)
    con_name = db.Column(db.String(64), nullable=False)
    level_id = db.Column(db.String(64), default=None)
    menu_id = db.Column(db.String(5000), default=None)
    library_id = db.Column(db.Text(65536))
    authority = db.Column(db.String(16), default=None)
    library_type = db.Column(db.Integer, nullable=False, default=0)
    con_type = db.Column(db.Integer, nullable=False, default=0)
    prev = db.Column(db.Integer, nullable=True, default=None)
    next = db.Column(db.Integer, nullable=True, default=None)
    code = db.Column(db.String(64), nullable=True, default=None)
    remark = db.Column(db.String(256), default=None)
    content_num = db.Column(db.Integer, default=None)
    char_num = db.Column(db.BigInteger, default=None)
    level_name = db.Column(db.String(255), default=None)
    user_id = db.Column(db.Integer, nullable=False, default=0)
    status = db.Column(db.Integer, default=None)
    label_id = db.Column(db.String(255), default=None)
    is_view = db.Column(db.Integer, nullable=False, default=0)
    is_vip = db.Column(db.Integer, default=1)
    label_tip = db.Column(db.Integer, default=0)


class TModules(model.BaseModel):
    __bind_key__ = 'writemall'
    __tablename__ = 't_modules'
    id = db.Column(db.Integer, primary_key=True, index=True)
    parent_id = db.Column(db.Integer, nullable=False)
    con_name = db.Column(db.String(255), nullable=False)
    name = db.Column(db.String(128), nullable=False)
    type = db.Column(db.Integer, nullable=True)
    base_path = db.Column(db.String(256), nullable=False)
    corpus_path = db.Column(db.String(256), nullable=True)
    vocab_path = db.Column(db.String(256), nullable=True)
    module_path = db.Column(db.String(256), nullable=True)
    module_code = db.Column(db.String(128), nullable=True)
    ctx_len = db.Column(db.Integer, nullable=False)
    n_epoch = db.Column(db.Integer, nullable=False)
    batch_size = db.Column(db.Integer, nullable=False)
    epoch_save_frequency = db.Column(db.Integer, nullable=False)
    model_level = db.Column(db.Integer, nullable=False)
    last_train_time = db.Column(db.DateTime)
    create_time = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False)
    train_num = db.Column(db.Integer, default=0, nullable=False)
    # progress = db.Column(db.Integer, nullable=False, default=0)
    init_load = db.Column(db.Integer, nullable=False, default=0)
    status = db.Column(db.Integer, nullable=True, default=1)
    corpus_determine = db.Column(db.Integer, nullable=False, default=0)
    remark = db.Column(db.String(512), nullable=True)


class TLibContextualRoute(model.BaseModel):
    __bind_key__ = 'elibrary'
    __tablename__ = 't_lib_contextual_route'
    id = db.Column(db.Integer, primary_key=True, index=True)
    type = db.Column(db.Integer, nullable=False)
    lib_key = db.Column(db.String(128), nullable=True)
    contextual_id = db.Column(db.String(128), nullable=True)
    status = db.Column(db.Integer, nullable=True)
    last_parse_time = db.Column(db.DateTime, nullable=True)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now())
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now())
    update_user = db.Column(db.Integer, nullable=False, default=None)
    is_delete = db.Column(db.Integer, nullable=False, default=0)


class MlRoute(model.BaseModel):
    __bind_key__ = 'writemall'
    __tablename__ = 't_ml_route'
    id = db.Column(db.Integer, primary_key=True, index=True)
    lib_key = db.Column(db.String(128), nullable=True)
    lib_name = db.Column(db.String(256), nullable=True)
    contextual_id = db.Column(db.String(128), nullable=True)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now())
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now())
    update_user = db.Column(db.Integer, nullable=False, default=None)
    is_delete = db.Column(db.Integer, nullable=False, default=0)


class LibCategories(model.BaseModel):
    __bind_key__ = 'elibrary'
    __tablename__ = 't_lib_categories'
    id = db.Column(db.Integer, primary_key=True, index=True)
    code = db.Column(db.String(512), nullable=False)
    p_id = db.Column(db.Integer, nullable=True)
    name = db.Column(db.String(2000), nullable=False)
    full_name = db.Column(db.String(10000), nullable=False)
    type = db.Column(db.Integer, nullable=False, default=0)
    status = db.Column(db.Integer, nullable=False, default=0)
    authoritative = db.Column(db.Numeric, default=3.0, nullable=True)
    book_quantity = db.Column(db.Integer, nullable=False, default=0)
    black_code = db.Column(db.Text(65535), nullable=True)
    orgin_code = db.Column(db.String(215), nullable=True)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now())
    sort = db.Column(db.Integer, nullable=True)
    audit = db.Column(db.Integer, nullable=True, default=0)
    audit_user = db.Column(db.String(255), nullable=True)
    user_id = db.Column(db.Integer, nullable=False)
    std_expr = db.Column(db.String(255), nullable=True)
